package com.wzy.netty.core.simple;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * 描述
 * 创建人    Wzy
 * 创建时间: 2020-05-21  14:56
 */
public class NettyClient {
    public static void main(String[] args) throws InterruptedException {
        //客户端需要一个时间循环组
        NioEventLoopGroup group = new NioEventLoopGroup();
        try {
            //创建一个启动对象
            Bootstrap bootstrap = new Bootstrap();

            bootstrap.group(group)//设置线程组
                    .channel(NioSocketChannel.class)//设置客户端通道的实现类
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new NettyClientHandler());//加入自定义处理器
                        }
                    });
            System.out.println("客户端 ... ok");
            //启动客户端去连接服务器端
            //关羽ChannelFuture要分析,涉及到netty 的异步模型
            ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 6668).sync();
            //给关闭通道进行监听
            channelFuture.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully();
        }

    }
}
